package com.klangner.go.app.rules;

import com.klangner.game.board.Move;


public class HumanGoPlayer extends Player implements Runnable, MoveInputDeviceListener {

  public HumanGoPlayer(int color) {
    super(color);

  }

  public void initPlayer() {}

  public void run() {
    // wait until a new attempt to play a move is made
    /*newMove = null;

    while (newMove == null)
      try {
        Thread.currentThread().sleep(100);
      } catch (InterruptedException ie) {
          System.err.println("Thread interrupted exceptinon in HumanGoPlayer: " + ie);
        }*/
  }

  protected Move move(BoardModel goPosition) {
    // here we should start a thread which monitors an input device
    // (mouse, etc.) to catch the moment when the player makes his move
    boolean newMoveMadeOK = false;
    newMove = null;

    while (newMoveMadeOK == false) {
    
    	//new Thread(this).start();
    	while (newMove == null){
        try {
          Thread.sleep(100);
        } catch (InterruptedException ie) {}
    	}
      
      // if we are here a new move attempt was made
      // check if it is a legal move
      if (goPosition.isLegalMove(newMove)) {
        newMoveMadeOK = true;
      }
      else {
        illegalMoveAnnouncement();
        newMove = null;
      }
    }
    
    return newMove;
  }

  public void setGame(GoGame game) {
    this.game = game;
  }

  /*
   * (non-Javadoc)
   * @see com.klangner.go.app.rules.MoveInputDeviceListener#moveAttempt(int, int)
   */
  public void moveAttempt(int x, int y) {
    newMove = new Move(x, y, getColor());
  }

  private void illegalMoveAnnouncement() {
    /*try {
    Toolkit.getDefaultToolkit().beep();
    } catch (Exception e) {}*/
  }

  //protected MoveInputDevice moveInputDevice;

  private boolean newMoveInput = false;
  private Move newMove = null;

  /*class WaitThread extends Thread {
    public void run() {

    }
  } */
}